$.namespace('dailyCheck');

//1.初始化&&声明一些函数响应按钮的操作
dailyCheck = function() {
	
	var urlPath = '';
	
	return {
		init : function() {
			$('#fmDlgPic').show().dialog('close');
			//EasyUIInit.initControls([dailyCheck.config.dailyCheck,dailyCheck.config.checkItem,dailyCheck.config.grade,dailyCheck.config.upload,dailyCheck.config.picPath,dailyCheck.config.commitRecord]);
			EasyUIInit.initControls([dailyCheck.config.dailyCheck,dailyCheck.config.dialog]);
			$('#grade').numberbox('setValue', 10);
			
			
			//该from是上传图片的form,嵌套在fm控件内
			$('#fmPic').ajaxForm({
				url: ctxRest + '/sys/file/upload',
				type: 'POST', 
				//dataType: 'json',
				resetForm: true,
				beforeSend : function(){
					progressLoad();
				},
				success: function (data) {
					progressClose();
					//图片返回的路径和名称放置在隐藏域里面
					$('#storePic').val(data);
					$('#storePic').linkbutton({text:'【上传图片成功!】'});
					$('#fmDlgPic').dialog('close');
					$('#imgs').html('<a target="_blank" href="' + ctxRest + '/sys/file/get?path=' + data + '" ><img src="' + ctxRest + '/sys/file/get?path=' + data + '" style="width:100px;height:100px;" /></a>');
				},
				
				error:function(data) {
					//上传图片附件发生错误....
					$('#storePic').linkbutton({text:'【上传图片失败!】'});
				}
			});
		},
		
		showForm : function() {
			$('#fmDlg').show().dialog('open');
			$('#fm').form('clear');
			urlPath = ctxRest + '/gcb/dailyCheck/create';
		},
		
		showEditForm : function() {
			var row = $('#dailyCheck').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要修改的数据.', 'info');
				return;
			}
			urlPath = ctxRest + '/gcb/dailyCheck/update';
			$('#fmDlg').show().dialog('open');
			$('#fm').form('load', row);
			$('#imgs').html('<a target="_blank" href="' + ctxRest + '/sys/file/get?path=' + row.storePic + '" ><img src="' + ctxRest + '/sys/file/get?path=' + row.storePic + '" style="width:100px;height:100px;"/></a>');
		},
		
		showPicForm : function() {
			$('#fmDlgPic').show().dialog('open');
			$('#fmPic').form('clear');
		},
		
		getNowFormatDate : function () {
		    var date = new Date();
		    var seperator1 = "-";
		    var seperator2 = ":";
		    var month = date.getMonth() + 1;
		    var strDate = date.getDate();
		    if (month >= 1 && month <= 9) {
		        month = "0" + month;
		    }
		    if (strDate >= 0 && strDate <= 9) {
		        strDate = "0" + strDate;
		    }
		    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
		            + " " + date.getHours() + seperator2 + date.getMinutes()
		            + seperator2 + date.getSeconds();
		    return currentdate;
		},
		
		
		
		getFormData : function(){
			var obj;
			var form = $('#fm');
			if (form.form('validate')) {
				obj = $.serializeObject(form);
			}
			return obj;
		},
		
		remove : function() {
			var row = $('#dailyCheck').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要删除的数据.', 'info');
				return;
			}
			$.messager.confirm('提示', '您确定要删除?', function(flag) {
				if (flag) {
					
					$.ajax({
						type : 'GET',
						contentType : 'application/json;charset=UTF-8',
						url : ctxRest + '/gcb/dailyCheck/delete?id=' + row.id,
						success : function(result) {
							$('#dailyCheck').datagrid('reload');
						}
					});
				}
			});
		},
		
		saveOrupdate : function()
		{
			var obj 		= dailyCheck.getFormData();
			//扣分项目名称
			obj.name		= $('#name').combobox('getText');//获取考核扣分的项目的名称
			//时间
			obj.checkDate   = dailyCheck.getNowFormatDate();
			//附件路径
			obj.storePic	= $('#storePic').val();					//图片路径 
			
			var	t	 		= $('#regionName').combotree('tree');  //获取树对象
			var n 			= t.tree('getSelected');			   //获取节点对象
			//在修改时,如果没有在combobox执行下拉选择动作,那么此时的N为NULL.
			if(n != null)
			{
				var parent 		= t.tree('getParent',n.target); 		//获取父节点对象
				obj.gridID 		= parent.id;							//父节点广场ID
				obj.gridName	= parent.name;							//父节点广场名称
				obj.regionID	= n.id;									//区域ID
				obj.regionName	=n.name;								//区域名称
			}
			
			
			if (obj) {
				$.ajax({
					type : 'POST',
					url : urlPath,
					data : JSON.stringify(obj),
					contentType : 'application/json;charset=UTF-8',
					success : function(result){
						if (result && result.errcode == 0) {
							$('#fmDlg').dialog('close');
							$('#dailyCheck').datagrid('reload');
						}
						else if (result.errcode == 1){
							$.messager.alert('提示', result.msg, 'info');
						}
					}
				})
			}
		},
	};
}();

// 2.界面必要的控件,依附于facility匿名函数对象
dailyCheck.config = {
		
		//1.dataGrid展示考核的扣分记录
		dailyCheck :{
			id		: "dailyCheck",
			type 	: "easyui-datagrid",
			option : {
				//title : '考核扣分列表'
				border:false,
				url : ctx + '/rest/gcb/dailyCheck/findList',
				contentType : 'application/json;charset=utf-8',
				idField : 'id',
				fitColumns : true,
				toolbar:"#toolbar",
				columns : [ [ {
					title : 'id',
					field : 'id',
					hidden: 'true',
					width : 30
				},{
					title : 'classCode',
					field : 'classCode',
					hidden: 'true',
					width : 30
				},{
					title : '考核项名称',
					field : 'name',
					width : 50
				},{
					title : '扣分',
					field : 'grade',
					width : 30
				},{
					title : '时间',
					field : 'checkDate',
					width : 30
				},{
					title : 'gridID',
					field : 'gridID',
					hidden: 'true',
					width : 30
				},{
					title : '广场名称',
					field : 'gridName',
					width : 30
				},{
					title : '区域名称',
					field : 'regionName',
					width : 30
				},{
					title : '备注信息',
					field : 'remark',
					width : 50
				}/*,{
					title : '图片',
					field : 'storePic',
					width : 50,
					formatter:function(value,row,index)
					{
						//{return '<img src="'+row.image+'" />';}
						return '<img style="width:30px;height=20px" src="'+ctxRest+'/sys/file/get?path='+row.storePic+'" />';
					}
				}*/]],
				toolbar: [{
					text:'增加',
					iconCls: 'icon-add',
					handler: dailyCheck.showForm,
				},'-',{
					text:'修改',
					iconCls: 'icon-edit',
					handler: dailyCheck.showEditForm,
				},'-',{
					text:'删除',
					iconCls: 'icon-remove',
					handler: dailyCheck.remove,
				}],
				
				onDblClickRow : dailyCheck.showEditForm
			}
		},
		
		//2.弹出的对话框
		dialog : {
			id : 'fmDlg',
			type : 'easyui-dialog',
			style : 'padding:5px;',
			option : {
				title : '考核信息',
				width : 380,
				height : 400 ,
				closed : true,
				buttons:[{
					text:'保  存',
					width:60,
					height:30,
					handler:dailyCheck.saveOrupdate
				},{
					text:'关  闭',
					width:60,
					height:30,
					handler:function(){
						$('#fmDlg').dialog('close');
					}
				}]
			},
			controls:[{
		  		id : 'fm',
				type : 'form',
				isExternal:true,
				style : 'padding:5px;',   
				layout : {
					captionWidth : 80,
					fieldWidth : 200,
					fieldHeight : 30,
					rowHeight : 38,
					columns : 1,
					controls : [
					            {id:'id',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            {id:'classCode',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            //考核项目名称 name
					            {id:'name',caption:'考核项名称',colspan:2,type:'easyui-combobox',
					            	option : {	
						    			url : ctx + '/rest/sys/basecode/getAll?classCode=("日常考核","个性化考核")',
						    			method : 'get',
						    			textField : 'name',
						    			valueField : 'code',
						    			editable : false,
						    			onSelect: function (record)
						    			{
						    				//facility.handleSelect(record);
						    				
						    			}
					    		}},
					            //扣分
					            {id:'grade',caption:'扣分',colspan:2,type:'easyui-numberspinner'},
					            //时间
					            {id:'checkDate',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            //gridID
					            {id:'gridID',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            //gridName
					            {id:'gridName',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            //regionID
					            {id:'regionID',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
					            //regionName
					            {id:'regionName',caption:'区域名称',colspan:2,type:'easyui-combotree',
					            	option : {
										method 		: 'post',
										url    		: ctx +'/rest/gcb/grid/findRegionList',
										contentType : 'application/json;charset=utf-8',
										idFiled 	: 'id',
										textFiled 	: 'name',
										parentField : 'pid',
										lines:true,
										},
										onLoadSuccess : function(node, data){
											$('#regionID').combotree('collapseAll');
										},
										onLoadError : function(arguments)
										{
											//console.log(arguments);
										}
								},
					            //备注
					            {id:'remark',caption:'备注信息',colspan:2,type:'easyui-textbox',option:{}},
					            
					            //上传图片
					            {id:'storePic',caption:'附件图片',colspan:2,type:'easyui-linkbutton',
					            	option : {
					            		required : true,
					            		editable : false,
					            		plain : false,
					            		text : '【上传考核扣分图片】',
					            		onClick : function() {
					            			dailyCheck.showPicForm()
					            		},
					            	}
					            },
					            {id:'imgs',caption:'图片：',type:'div',isExternal:true,colspan:2}
				            ]
				}
			}]
		}
};
// 3.初始化
$(function() {
	dailyCheck.init();
})